package com.monster.servlet.home;

import com.monster.dao.UserFocusDao;
import com.monster.dao.UserFocusDaoImpl;
import com.monster.service.UserLikeService;
import com.monster.service.UserLikeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/UserFocusServlet")
public class UserFocusServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        String no1 = request.getParameter("focusId");
        Long focusId = Long.parseLong(no1);
        String no2 = request.getParameter("userId");
        Long userId = Long.parseLong(no2);
        String no3 = request.getParameter("focusType");
        int focusType = Integer.parseInt(no3);
        String result = null;;
        switch (focusType){
            //用户类型关注
            case 1:
                result = combine(userId,focusType,focusId);
                break;
            //歌手类型关注
            case 2:
                result = combine(userId,focusType,focusId);
                break;
        }
        out.print(result);
        out.flush();
        out.close();
    }

    //查询是否存在，存在调用删除，不存在调用添加
    public String combine (Long userId, int focusType, Long focusId){
        String result = null;
        if(exist(userId,focusType,focusId)==true){
            if(delete(userId,focusType,focusId)==true){
                result = "{\"data\":\"NO\"}";
            }else {
                result = "{\"data\":\"ERROR\"}";
            }
        }else{
            if(add(userId,focusType,focusId)==true) {
                result = "{\"data\":\"YES\"}";
            }else {
                result = "{\"data\":\"ERROR\"}";
            }
        }
        return  result;
    }

    //查看该收藏是否存在
    public boolean exist (Long userId, int focusType, Long focusId){
        UserFocusDao userFocusDao = new UserFocusDaoImpl();
        boolean flag = false;
        int result = 0 ;
        result = userFocusDao.getFocusCountByID(userId,focusType,focusId);
        if(result==1){
            flag=true;
            return flag;
        }
        else{
            return flag;
        }
    }

    //取消收藏
    public boolean delete (Long userId, int focusType, Long focusId){
        UserFocusDao userFocusDao = new UserFocusDaoImpl();
        boolean flag = false;
        int result = 0 ;
        result = userFocusDao.deleteFocusById(userId,focusType,focusId);
        if(result==1){
            flag=true;
            return flag;
        }
        else{
            return flag;
        }
    }

    //添加收藏
    public boolean add (Long userId, int focusType, Long focusId){
        UserFocusDao userFocusDao = new UserFocusDaoImpl();
        boolean flag = false;
        int result = 0 ;
        result = userFocusDao.addFocusByID(userId,focusType,focusId);
        if(result==1){
            flag=true;
            return flag;
        }
        else{
            return flag;
        }
    }
}
